import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DownloadThread extends Thread{
    public String date;

    DownloadThread (String date) {
        this.date = date;
    }

    public void run () {
        System.out.println(this.date);
        CloseableHttpClient htmlClient = HttpClientBuilder.create().build();
        CloseableHttpResponse htmlResponse = null;
        CloseableHttpClient imageClient = HttpClientBuilder.create().build();
        CloseableHttpResponse imageResponse = null;

        try {
            HttpGet htmlGet = new HttpGet("https://bobopic.com/pixiv-" + this.date + ".html");
            htmlGet.addHeader("referer", "https://go.bobopic.com/");
            htmlGet.addHeader("cookie", "Hm_lvt_cd79d673082e9aa6447202dcddd8a656=1660793597,1660807901; Hm_lpvt_cd79d673082e9aa6447202dcddd8a656=1660807966");
            htmlGet.addHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.81 Safari/537.36 Edg/104.0.1293.54");
            htmlResponse = htmlClient.execute(htmlGet);
            if (htmlResponse == null) {
                System.out.println("请求失败");
            } else {
                Integer code = htmlResponse.getStatusLine().getStatusCode();
                if (code == 404) {
                    System.out.println("不存在该日期排行榜: " + this.date);
                } else if (code == 403) {
                    System.out.println("请求不到了!");
                } else if (code == 200){
                    System.out.println("下载日期排行榜:" + this.date);
                    String res = EntityUtils.toString(htmlResponse.getEntity());
                    Pattern pattern = Pattern.compile("https://i.bobopic.com/small/.*/></p>");
                    Matcher m = pattern.matcher(res);
                    while (m.find()) {
                        String fileName = m.group().replace("\"/></p>", "").replace("https://i.bobopic.com/small/", "");
                        String imageUrl = "https://ii.bobopic.com/tu/" + fileName;
                        HttpGet imageGet = new HttpGet(imageUrl);
                        imageGet.addHeader("referer", "https://go.bobopic.com/");
                        imageGet.addHeader("cookie", "Hm_lvt_cd79d673082e9aa6447202dcddd8a656=1660793597,1660807901; Hm_lpvt_cd79d673082e9aa6447202dcddd8a656=1660812197");
                        imageGet.addHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.81 Safari/537.36 Edg/104.0.1293.54");
                        imageResponse = imageClient.execute(imageGet);
                        if (imageResponse != null && imageResponse.getStatusLine().getStatusCode() == 200) {
                            byte[] bytes = EntityUtils.toByteArray(imageResponse.getEntity());
                            String downloadPath = "D:\\pictures\\" + fileName;
                            FileOutputStream fos = new FileOutputStream(downloadPath);
                            fos.write(bytes);
                            fos.close();
                            System.out.println("图片下载成功: " + imageUrl);
                        } else {
                            System.out.println("图片下载失败: " + imageUrl);
                        }
                    }
                    System.out.println("图片下载线程结束: " + this.date);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 关闭连接释放资源
                if (htmlResponse != null) {
                    htmlResponse.close();
                }
                if (htmlClient != null) {
                    htmlClient.close();
                }
                if (imageResponse != null) {
                    htmlResponse.close();
                }
                if (imageClient != null) {
                    htmlClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
